package com.phenix.wanandroid.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.phenix.network.WanAndroidOpenApi
import com.phenix.network.response.NetworkResponse
import com.phenix.wanandroid.api.WanAndroidApiInterface
import kotlinx.coroutines.launch

class MainViewModel : ViewModel() {

    val hello: MutableLiveData<String> by lazy {
        MutableLiveData<String>("Android")
    }

    companion object {
        private const val TAG = "MainViewModel"
    }

    fun getMavenPomList(k: String) {
        Log.i(TAG, "call -> getMavenPomList($k)")
        viewModelScope.launch {
            when (val result =
                WanAndroidOpenApi.getService(WanAndroidApiInterface::class.java).getMavenPom(k)) {
                is NetworkResponse.Success<*> -> {
                    Log.i(TAG, "call result->${result.body}")
                    hello.value =result.body.toString()
                }
                is NetworkResponse.ApiError -> {
                    Log.e(TAG, "ApiError:${result.code}, ${result.msg}")
                }
                is NetworkResponse.NetworkError -> {
                    Log.e(TAG, "NetworkError:${result.code}, ${result.msg}")
                }
                is NetworkResponse.UnknownError -> {
                    Log.e(TAG, "UnknownError:$result")
                }
            }
        }
    }
}